<?php

class ExercisesController extends \Phalcon\Mvc\Controller {

    public function indexAction() {
        $excercises = Tests::find();
        $this->view->excercises = $excercises;
    }

    public function createAction() {
        if ($this->request->isPost()) {
            if ($this->request->getPost('part')) {
                $partNumber = $this->request->getPost('part');
                $exerecire = new Tests();
                $questionsList = array();

                echo '<pre>';
                for ($i = 1; $i <= 7; $i++) {
                    $parameters = array(
                        "part = '$i'",
                        'order' => 'RAND()',
                        'limit' => $partNumber[$i],
                    );

                    switch ($i) {
                        case 1:
                        case 2:
                            $questions = Questions::find($parameters);
                            $questionsList = array_merge($questionsList, $questions->toArray());
                            break;
                        case 3:
                        case 4:
                            $audios = Audios::find($parameters);
                            foreach ($audios as $audio) {
                                $questions = Questions::findByAudio($audio->id);
                                $questionsList = array_merge($questionsList, $questions->toArray());
                            }
                            break;
                        case 5:
                            $questions = Questions::find($parameters);
                            $questionsList = array_merge($questionsList, $questions->toArray());
                            break;
                        case 6:
                            $references = Reference::find($parameters);
                            foreach ($references as $reference) {
                                $questions = Questions::findByReference($reference->id);
                                $questionsList = array_merge($questionsList, $questions->toArray());
                            }
                            break;
                        case 7:
                            $count = 0;
                            $parameters[0] = "part = '$i'";
                            $parameters['limit'] = 14;
                            $references = Reference::find($parameters);
                            foreach ($references as $referenceKey => $reference) {
                                $questions = Questions::findByReference($reference->id);
                                foreach ($questions as $questionKey => $question) {
                                    if (($questionKey >= 2 && $questionKey > (count($questions) - 2) && $count == ($partNumber[7][1] - 2)) || $count == $partNumber[7][1]) {
                                        break;
                                    } else {
                                        array_push($questionsList, $question->toArray());
                                        $count++;
                                    }
                                }
                                if ($count == ($partNumber[7][1] - 2)) {
                                    break;
                                }
                            }

                            $parameters[0] = "part = '$i' AND paragraphs = '2'";
                            $parameters['limit'] = $partNumber[7][2];
                            $references = Reference::find($parameters);
                            foreach ($references as $reference) {
                                $questions = Questions::findByReference($reference->id);
                                $questionsList = array_merge($questionsList, $questions->toArray());
                            }
                            break;
                        default:
                            break;
                    }
                }
                print_r($questionsList);
            }
        }
    }

}
